#include "Receiver.h"
#include "Output.h"
#define SAMP_NUMBER 4
bool cycle_done=0;
u16 ADC_Value[SAMP_NUMBER];
u16 Action =1230;//1.5v
u16 Quit=1150;//1.4

static void  Adc_Init()
{

	
	ADC_InitType ADC_InitStructure;
    ADC_InitStructure.MultiChEn      = DISABLE;
    ADC_InitStructure.ContinueConvEn = DISABLE;
    ADC_InitStructure.DatAlign       = ADC_DAT_ALIGN_R;
	ADC_InitStructure.ExtTrigSelect  = ADC_EXT_TRIGCONV_T1_CC2;
	ADC_InitStructure.ChsNumber      = 1;
    ADC_Init(ADC, &ADC_InitStructure);   
   	
	ADC_ConfigRegularChannel(ADC, ADC_CH_0_PA0, 1, ADC_SAMP_TIME_6CYCLES5);
	  
	ADC_EnableExternalTrigConv(ADC, ENABLE);//ADC->CTRL2|=((uint32_t)0x00100000);
	
	  		          
    ADC_EnableDMA(ADC, ENABLE);
	ADC_Enable(ADC, ENABLE);
    while(ADC_GetFlagStatusNew(ADC,ADC_FLAG_RDY) == RESET)
        ;
    while(ADC_GetFlagStatusNew(ADC,ADC_FLAG_PD_RDY));
    
    
    
    
    


}
static void DMA_Inition()
{

    DMA_InitType      DMA_InitStructure;
	DMA_DeInit(DMA_CH2);
    DMA_InitStructure.PeriphAddr     = (uint32_t)&ADC->DAT;
    DMA_InitStructure.MemAddr        = (uint32_t)&ADC_Value;
    DMA_InitStructure.Direction      = DMA_DIR_PERIPH_SRC;
	
    DMA_InitStructure.BufSize        = SAMP_NUMBER ;
	
    DMA_InitStructure.PeriphInc      = DMA_PERIPH_INC_DISABLE;
    DMA_InitStructure.DMA_MemoryInc  = DMA_MEM_INC_ENABLE;		
		
    DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
    DMA_InitStructure.MemDataSize    = DMA_MemoryDataSize_HalfWord;
		
		
    DMA_InitStructure.CircularMode   = DMA_MODE_CIRCULAR;
    //DMA_InitStructure.CircularMode   = DMA_MODE_NORMAL;		
		
	DMA_InitStructure.Priority       = DMA_PRIORITY_HIGH;
    DMA_InitStructure.Mem2Mem        = DMA_M2M_DISABLE;
    DMA_Init(DMA_CH2, &DMA_InitStructure);
    
	DMA_RequestRemap(DMA_REMAP_ADC, DMA, DMA_CH2, ENABLE);
    /* Enable DMA channel1 */
	DMA_ConfigInt(DMA_CH2,DMA_INT_TXC,ENABLE);
		
    DMA_EnableChannel(DMA_CH2, ENABLE);


}

void Receiver_Init(void)
{
 
    Adc_Init();
    DMA_Inition();


}
void OUT_PROCESS()
{
		if(ADC_Value[0]>Action&&ADC_Value[1]>Action&&ADC_Value[2]>Action&&ADC_Value[3]>Action)
					  {
						
                      GPIO_SetBits(Indicator_Light_Port, Indicator_Light_Pin);
                     

                          
					  }	
      else   if(ADC_Value[0]<Quit&&ADC_Value[1]<Quit&&ADC_Value[2]<Quit&&ADC_Value[3]<Quit)
				 {
				 
				         GPIO_ResetBits(Indicator_Light_Port, Indicator_Light_Pin);       
				 
				 }					 






}